/*
 * Report.java
 * Created on Apr 13, 2005 5:59:34 PM
 */


/**
 * @author Andry
 * The Report.java class provides
 *
 */
package edu.mobbuzz.messaging;
public class Report {
    public JID recipient;
    public TimeStamp sent;
    public TimeStamp delivered;
    
    
    /**
     * @param recipient		Recipient JID
     * @param sent			TimeStamp of message sent
     * @param delivered		TimeStamp of message delivered
     */
    public Report(JID recipient, TimeStamp sent, TimeStamp delivered) {
        this.recipient = recipient;
        this.sent = sent;
        this.delivered = delivered;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
        /*StringBuffer output = new StringBuffer(recipient.toStringWithoutResource());
        output.append('\n');
        output.append(sent.toStringUnformatted());
        output.append('\n');
        output.append(delivered.toStringUnformatted());
        return (output.toString()); */
        return (recipient.toStringWithoutResource() + "\n" + sent.toStringUnformatted() + "\n" + delivered.toStringUnformatted());
    }
    
    /**
     * Parse string of Report generated by toString() method
     * @param report		String of Report
     * @param timezone		GMT timezone
     * @return	Report object represented by report
     */
    public static Report parseReport (String report, int timezone){
        int start = 0;
        int end;
        String recipient;
        String sent;
        String delivered;
        
        // Get first line containing recipient JID
        end = report.indexOf("\n", start);
        recipient = report.substring(start, end);
        // Get second line containing message sent TimeStamp
        start = end + 1;
        end = report.indexOf("\n", start);
        sent = report.substring(start, end);
        // Get last line containing message delivered TimeStamp
        delivered = report.substring(end + 1);
        return (new Report(JID.parseJIDString(recipient),
                			TimeStamp.parseTimeStringUnformatted(sent, timezone),
                			TimeStamp.parseTimeStringUnformatted(delivered, timezone)));
    }
}
